home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld Secrets (4th Edition)
/
Mac Secrets CD 4th Ed.toast
/
Apple Advanced Technologies
/
Apple Speech Technologies 1.5
/
PlainTalk Developer Info
/
Speech Recognition Manager SDK
/
SR Sample Code
/
Weapons Example
/
SpeechWeapons.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-04-11
|
6KB
|
204 lines
#include <SpeechRecognition.h>
#include "string.h"
#include "SpeechWeapons.h"
#include "Weapons.h"
// constants
const long kWeaponLMRefCon = 'weap';
// globals
SRRecognitionSystem gRecSystem = 0;
SRLanguageModel gLanguageModel = 0;
SRRecognizer gRecognizer = 0;
// prototypes
Boolean MyHasSpeechRecognitionMgr (void);
pascal OSErr MyHandleSpeechDoneAppleEvent (AppleEvent *theAEevt, AppleEvent* reply, long refcon);
OSErr MyBuildLanguageModel (SRRecognitionSystem mySystem, SRLanguageModel * languageModel);
void MyHandleRecognitionResult (SRRecognitionResult recResult);
void MyInitSpeechRecognition (void)
{
OSErr myErr = noErr;
if (!MyHasSpeechRecognitionMgr())
myErr = true;
/* Install AppleEvent handler so recognizer can send us speech-done AppleEvents. */
if (!myErr)
myErr = AEInstallEventHandler(kAESpeechSuite, kAESpeechDone,
NewAEEventHandlerProc(MyHandleSpeechDoneAppleEvent), 0, false);
if (!myErr)
myErr = SROpenRecognitionSystem (&gRecSystem, kSRDefaultRecognitionSystemID);
if (!myErr)
{
short myModes = kSRHasFeedbackHasListenModes;
myErr = SRSetProperty (gRecSystem, kSRFeedbackAndListeningModes,
&myModes, sizeof (myModes));
}
if (!myErr)
myErr = MyBuildLanguageModel (gRecSystem, &gLanguageModel);
if (!myErr)
myErr = SRNewRecognizer (gRecSystem, &gRecognizer, kSRDefaultSpeechSource);
if (!myErr)
myErr = SRSetLanguageModel (gRecognizer, gLanguageModel);
if (!myErr)
myErr = SRStartListening (gRecognizer);
}
void MyCleanupSpeechRecognition (void)
{
OSErr myErr = noErr;
if (gLanguageModel)
myErr = SRReleaseObject (gLanguageModel);
if (gRecognizer)
{
myErr = SRStopListening (gRecognizer); /* stop processing incoming sound */
myErr = SRReleaseObject (gRecognizer); /* balance SRNewRecognizer call */
}
if (gRecSystem)
myErr = SRCloseRecognitionSystem (gRecSystem); /* balance SROpenRecognitionSystem call */
}
Boolean MyHasSpeechRecognitionMgr (void)
{
OSErr myErr;
long mySRVersion;
Boolean myHasSRMgr = false;
myErr = Gestalt (gestaltSpeechRecognitionVersion, &mySRVersion);
if (!myErr)
if (mySRVersion >= 0x0150)
myHasSRMgr = true;
return myHasSRMgr;
}
pascal OSErr MyHandleSpeechDoneAppleEvent (AppleEvent *theAEevt, AppleEvent* reply, long refcon)
{
long actualSize;
DescType actualType;
OSErr status = 0, recStatus = 0;
SRRecognitionResult recResult = 0;
/* Get status */
status = AEGetParamPtr (theAEevt, keySRSpeechStatus, typeShortInteger,
&actualType, (Ptr)&recStatus, sizeof(status), &actualSize);
/* Get result and handle it */
if (!status)
{
status = AEGetParamPtr(theAEevt,keySRSpeechResult,
typeSRSpeechResult, &actualType, (Ptr)&recResult,
sizeof(SRRecognitionResult), &actualSize);
if (!recStatus)
MyHandleRecognitionResult (recResult);
}
return status;
}
/* MyBuildLanguageModel creates language model for the following BNF:
<weapon> = Bowie Knife | Forty-Four Pistol | Machine Gun |
Double Barrel Shotgun | Flame Thrower | Rocket launcher;
*/
OSErr MyBuildLanguageModel (SRRecognitionSystem mySystem, SRLanguageModel * languageModel)
{
OSErr myErr = noErr;
SRLanguageModel myWeaponLM = 0;
SRPath myPath;
char ** myStringArray;
char * myCurrString;
// create an embedded language model named "<weapon>"
if (!myErr)
myErr = SRNewLanguageModel (mySystem, &myWeaponLM, "<weapon>", strlen("<weapon>"));
if (!myErr)
myErr = SRSetProperty (myWeaponLM, kSRRefCon, &kWeaponLMRefCon, sizeof(kWeaponLMRefCon));
if (!myErr)
myErr = SRAddText (myWeaponLM, "Bowie Knife", strlen("Bowie Knife"), kKnife);
if (!myErr)
myErr = SRAddText (myWeaponLM, "Forty-Four Pistol", strlen("Forty-Four Pistol"), kPistol);
if (!myErr)
myErr = SRAddText (myWeaponLM, "Machine Gun", strlen("Machine Gun"), kMachineGun);
if (!myErr)
myErr = SRAddText (myWeaponLM, "Double Barrel Shotgun", strlen("Double Barrel Shotgun"), kShotgun);
if (!myErr)
myErr = SRAddText (myWeaponLM, "Flame Thrower", strlen("Flame Thrower"), kFlameThrower);
if (!myErr)
myErr = SRAddText (myWeaponLM, "Rocket launcher", strlen("Rocket launcher"), kRocketLauncher);
if (!myErr)
*languageModel = myWeaponLM;
return myErr;
}
void MyHandleRecognitionResult (SRRecognitionResult recResult)
{
OSErr myErr = noErr;
if (recResult)
{
SRLanguageModel myResultLM;
Size myLen;
myLen = sizeof (myResultLM);
myErr = SRGetProperty (recResult, kSRLanguageModelFormat, &myResultLM, &myLen);
if (!myErr)
{
long myRefCon;
myLen = sizeof (myRefCon);
myErr = SRGetProperty (myResultLM, kSRRefCon, &myRefCon, &myLen);
/* if it's a valid result from our weapon LM, */
/* then parse and process its elements */
if (!myErr && myRefCon == kWeaponLMRefCon)
{
SRLanguageObject mySubElement;
/* weapon LM just a list of weapons: */
/* we get the first sub-element (there will only be one, the phrase) */
myErr = SRGetIndexedItem (myResultLM, &mySubElement, 0);
if (!myErr)
{
myLen = sizeof (myRefCon);
myErr = SRGetProperty (mySubElement, kSRRefCon, &myRefCon, &myLen);
/* we previously set the refcon to the weapon ID */
SetWeapon (myRefCon);
/* release subelement when done with it */
myErr = SRReleaseObject (mySubElement);
}
}
// release myResultLM fetched above when done with it
myErr = SRReleaseObject (myResultLM);
}
/* Release SRRecognitionResult since we are done with it */
/* The toolbox has given us a reference to this object, and */
/* assumes we are still using it until we call SRReleaseObject */
/* with it. */
myErr = SRReleaseObject (recResult);
}
}